package com.yxuan.springboot.controller;

import com.yxuan.springboot.CustomizedException.DefaultException;
import com.yxuan.springboot.model.SchoolInformation;
import com.yxuan.springboot.model.ThirdToken;
import com.yxuan.springboot.service.ThirdTokenService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.yxuan.springboot.controller.DouyinPayController.getDDpost;

@Slf4j
@Controller
@Api(tags = "管理员登录表")
@RequestMapping(value = "/a", method = {RequestMethod.POST,RequestMethod.GET})
public class AController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    //    测试MQ
    @RequestMapping("/ab")
    @ResponseBody
    public void a(){

        // 交换机名称
        String exchange = "wxzhifu.exchange";
        for (int i = 0; i < 6; i++) {
            log.info("发送消息");
            rabbitTemplate.convertAndSend(exchange,"wxt", i+"aaaaa");
        }
        // 发送消息

    }

    @Autowired
    private ThirdTokenService thirdTokenService;


    @Resource(name = "wxPayClient")
    private CloseableHttpClient dyPayClient;
    @Value("${douyin.appid}")
    private String DY_APP_ID;
    @Value("${douyin.appsecret}")
    private String DY_APP_SECRET;

    @RequestMapping("/a")
    @ResponseBody
    public String AutoReloadDouYinToken() throws Exception {



        Map<String, Object> parame = new LinkedHashMap<>();
        //抖音小程序app
        parame.put("appid", DY_APP_ID);
        // 商户订单编号
        parame.put("secret", DY_APP_SECRET);

        parame.put("grant_type", "client_credential");        //小程序用户的 open_id，通过 code2Session 获取


        try {
            Map<String,Object> getpost = getDDpost(parame, dyPayClient);
            String accessToken = (String) ((Map<String, Object>)getpost.get("data")).get("access_token");
            ThirdToken firm = new ThirdToken();
            firm.setAccessToken(accessToken);
            firm.setTokenType("抖音小程序token");
            thirdTokenService.upd(firm);
            return accessToken;

        } catch (DefaultException e) {
            e.printStackTrace();
            return null;
        }





    }




}
